#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//

typedef struct Polyomial {
	int coeff;
	int exponent;
} Poly;

typedef struct LinkedList {
	Poly *data;
	struct LinkedList *next;
} LList;

//����ͷ���
LList *InitHead() {
	LList *newnode = (LList *)malloc(sizeof(LList));
	if (newnode == NULL)
		return NULL;
	newnode->data = (Poly *)malloc(sizeof(Poly));
	if (NULL == newnode)
		return NULL;
	newnode->next = NULL;

	return newnode;
}

//�����½ڵ�
LList *BuyNewnode(int coeff, int exponent) {
	LList *newnode = (LList *)malloc(sizeof(LList));
	if (newnode == NULL)
		return NULL;
	newnode->data = (Poly *)malloc(sizeof(Poly));
	if (NULL == newnode)
		return NULL;
	newnode->data->coeff = coeff;
	newnode->data->exponent = exponent;
	newnode->next = NULL;

	return newnode;
}

//β�����룬��˳��
void LListPushBack(LList *phead, int coeff, int exponent) {
	assert(phead);
	LList *newnode = BuyNewnode(coeff, exponent);
	LList *tail = phead;
	while (tail->next != NULL) {
		tail = tail->next;
	}
	tail->next = newnode;
	newnode = NULL;
}

//��ӡ����ʽ
void Print(LList *phead) {
	if (phead == NULL)
		return;
	LList *cur = phead->next;
	int count = 1;
	while (cur) {
		if (0 == cur->data->coeff)
			goto end;
		if (count == 1) {
			if (cur->data->exponent == 0)
				printf("%d", cur->data->coeff);
			else if (cur->data->exponent == 1) {
				if (cur->data->coeff > 0) {
					if (cur->data->coeff == 1)
						printf("x");
					else
						printf("%dx", cur->data->coeff);
				} else {
					if (cur->data->coeff == -1)
						printf("-x");
					else
						printf("%dx", cur->data->coeff);
				}
			} else {
				if (cur->data->coeff > 0) {
					if (cur->data->coeff == 1)
						printf("x^%d", cur->data->exponent);
					else
						printf("%dx^%d", cur->data->coeff, cur->data->exponent);
				} else {
					if (cur->data->coeff == -1)
						printf("-x^%d", cur->data->exponent);
					else
						printf("%dx^%d", cur->data->coeff, cur->data->exponent);
				}
			}
		} else {
			if (cur->data->exponent == 1) {
				if (cur->data->coeff > 0) {
					if (cur->data->coeff == 1)
						printf("+x");
					else
						printf("%+dx", cur->data->coeff);
				} else {
					if (cur->data->coeff == -1)
						printf("-x");
					else
						printf("%dx", cur->data->coeff);
				}
			} else {
				if (cur->data->coeff > 0) {
					if (cur->data->coeff == 1)
						printf("+x^%d", cur->data->exponent);
					else
						printf("%+dx^%d", cur->data->coeff, cur->data->exponent);
				} else {
					if (cur->data->coeff == -1)
						printf("-x^%d", cur->data->exponent);
					else
						printf("%dx^%d", cur->data->coeff, cur->data->exponent);
				}
			}
		}
		count++;
end:
		cur = cur->next;
	}

	printf("\n");
}

void LListtest1() {
	LList *plist = (LList *)malloc(sizeof(LList));
	if (plist == NULL)
		return;
	plist->data = NULL, plist->next = NULL;
	LListPushBack(plist, 5, 3);
	Print(plist);
	LListPushBack(plist, 5, 3);
	Print(plist);
	LListPushBack(plist, 5, 3);
	Print(plist);
}

void LListtest2() {
	LList *plist = (LList *)malloc(sizeof(LList));
	if (plist == NULL)
		return;
	plist->data = NULL, plist->next = NULL;
	LListPushBack(plist, 5, 3);
//	Print(plist);
	LListPushBack(plist, 4, 6);
//	Print(plist);
	LListPushBack(plist, 9, 7);
	Print(plist);
}

//��������ʽ
void CreatPoly(LList *phead, int *data, int datasize) {
	int count = 0;
	while (count < datasize) {
		LListPushBack(phead, data[count], data[count + 1]);
		count += 2;
	}
}
//�����ڵ�ϵ����ָ��
void swap(Poly **poly1, Poly **poly2) {
	int t = (*poly1)->coeff;
	(*poly1)->coeff = (*poly2)->coeff;
	(*poly2)->coeff = t;
	t = (*poly1)->exponent;
	(*poly1)->exponent = (*poly2)->exponent;
	(*poly2)->exponent = t;
}
//����ʽ�ӷ�
LList *AddPoly(LList *Poly1, LList *Poly2) {
	LList *cur1 = Poly1->next;
	LList *cur2 = Poly2->next;
	if (cur1 == NULL && cur2 == NULL)
		return NULL;
	else if (cur2 == NULL)
		return Poly1;
	else if (cur1 == NULL)
		return Poly2;

	while (cur1 && cur2) {
		if (cur1->data->exponent == cur2->data->exponent) {
			cur1->data->coeff += cur2->data->coeff;
			cur1 = cur1->next;
			cur2 = cur2->next;

		} else if (cur1->data->exponent < cur2->data->exponent) {
			cur1 = cur1->next;

		} else {
			LList *temp = cur2->next;
			cur2->next = cur1->next;
			cur1->next = cur2;
			swap(&(cur1->data), &(cur2->data));
			cur2 = temp;
			cur1 = cur1->next->next;
		}
	}
	cur1 = Poly1->next;

	if (cur2) {
		while (cur1->next != NULL)
			cur1 = cur1->next;
		cur1->next = cur2;
	}

	return Poly1;
}

//������ʽϵ����Ϊ��
void ChangePoly(LList *poly) {
	LList *cur = poly->next;
	while (cur) {
		cur->data->coeff *= -1;
		cur = cur->next;
	}
}
//����ʽ����
LList *SubPoly(LList *Poly1, LList *Poly2) {
	assert(Poly1);
	assert(Poly2);
	LList *cur1 = Poly1->next;
	LList *cur2 = Poly2->next;
	if (cur1 == NULL && cur2 == NULL)
		return NULL;

	else if (cur2 == NULL) {
		return Poly1;








	} else if (cur1 == NULL) {
		ChangePoly(Poly2);
		return Poly2;
	}

	while (cur1 && cur2) {
		if (cur1->data->exponent == cur2->data->exponent) {
			cur1->data->coeff -= cur2->data->coeff;
			cur1 = cur1->next;
			cur2 = cur2->next;

		} else if (cur1->data->exponent < cur2->data->exponent) {
			cur1 = cur1->next;

		} else {
			cur2->data->coeff *= -1;
			LList *temp = cur2->next;
			cur2->next = cur1->next;
			cur1->next = cur2;
			swap(&(cur1->data), &(cur2->data));
			cur2 = temp;
			cur1 = cur1->next->next;
		}
	}
	cur1 = Poly1->next;

	if (cur2) {
		LList *temp = cur2;
		while (temp) {
			temp->data->coeff *= -1;
			temp = temp->next;
		}
		while (cur1->next != NULL)
			cur1 = cur1->next;
		cur1->next = cur2;
	}

	return Poly1;
}


int main() {
	int n, m, t;
	LList *Poly1 = InitHead();
	LList *Poly2 = InitHead();
	n = m = t = 0;
	scanf("%d%d%d", &n, &m, &t);
	int *data1 = NULL;
	if (n != 0) {
		data1 = (int *)malloc(2 * n * sizeof(LList));
		if (data1 == NULL)
			return -1;
		for (int i = 0; i < 2 * n; i++)
			scanf("%d", &data1[i]);
	}
	CreatPoly(Poly1, data1, 2 * n);
	int *data2 = NULL;

	if (m != 0) {
		data2 = (int *)malloc(2 * m * sizeof(LList));
		if (data2 == NULL)
			return -1;
		for (int i = 0; i < 2 * m; i++)
			scanf("%d", &data2[i]);
	}
	CreatPoly(Poly2, data2, 2 * m);
	LList *result = NULL;
	if (0 == t)
		result = AddPoly(Poly1, Poly2);
	else if (1 == t)
		result = SubPoly(Poly1, Poly2);
	Print(result);

	return 0;
}